<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <child1></child1>
        <child2></child2>
    </div>
    <script src="./vue.js"></script>
    <script>
        const child1 = {
            template:  `
                <div>
                    这是子组件
                    <button @click="send">点击传递数据给父组件</button>
                </div>
            `,
            data() {
                return {
                    msg: '这是要传递给父组件的数据'
                }
            },

            methods: {
                send() {
                    this.$bus.$emit('getdata', this.msg)
                }
            }
        }

        const child2 = {
            template:  `
                <div>
                    这是子组件
                    {{msg}}
                </div>
            `,
            created() {
                // console.log(this)
                // var _this = this
                this.$bus.$on('getdata', (data) => {
                    // console.log(this)
                    this.msg = data
                })
            },
            data() {
                return {
                    msg: ''
                }
            }
        }
        Vue.component('child1', child1)
        Vue.component('child2', child2)
        var bus = new Vue()
        Vue.prototype.$bus = bus
        new Vue({
            methods: {

            }
        }).$mount('#app')
    </script>
</body>
</html>